﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LineCommandProcess.cs" company="">
//   
// </copyright>
// <summary>
//   Defines the LineCommandProcess type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace SQLMetalOSUICtrl
{
    using System;

    /// <summary>
    /// </summary>
    internal class LineCommandProcess
    {
        /// <summary>
        /// </summary>
        private System.Diagnostics.Process _command;

        /// <summary>
        /// Initializes a new instance of the <see cref="LineCommandProcess"/> class.
        /// </summary>
        /// <param name="sqlmetal">
        /// The sqlmetal.
        /// </param>
        internal LineCommandProcess(SQLMetalTool sqlmetal)
        {
            _command =
                new System.Diagnostics.Process
                    {
                        EnableRaisingEvents = false,
                        StartInfo =
                            {
                                WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
                                CreateNoWindow = true,
                                RedirectStandardOutput = true,
                                RedirectStandardInput = false,
                                UseShellExecute = false,
                                FileName = sqlmetal.Path
                            }
                    };
        }

        /// <summary>
        /// </summary>
        /// <param name="parameters">
        /// The parameters.
        /// </param>
        /// <returns>
        /// </returns>
        internal string ExecuteWithParameters(string parameters)
        {
            try
            {
                _command.StartInfo.Arguments = parameters;
                _command.Start();
                return _command.StandardOutput.ReadToEnd();
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }

        /// <summary>
        /// </summary>
        /// <returns>
        /// </returns>
        internal string ExecuteWithNoParameters()
        {
            try
            {
                _command.Start();
                return _command.StandardOutput.ReadToEnd();
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }

        /// <summary>
        /// </summary>
        /// <returns>
        /// </returns>
        internal string ExecuteForInformationPruposes()
        {
            var result = string.Empty;

            try
            {
                _command.Start();
                //only takes the 3 first lines
                result = _command.StandardOutput.ReadLine() + Environment.NewLine;
                result += _command.StandardOutput.ReadLine() + Environment.NewLine;
                result += _command.StandardOutput.ReadLine();
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }

            return result;

        }

    }
}
